package com.xgsm.Fmmall.controller;

import com.xgsm.Fmmall.entity.Users;
import com.xgsm.Fmmall.service.UserService;
import com.xgsm.Fmmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

//@ResponseBody 异步请求，返回的是json数据
/*
 * swagger提供了一套注解，可以对接口进行详细的说明
 * 1、Api类注解，对控制器类进行功能说明
 * 2、@ApiOperation方法注解，对控制器方法进行说明
 * 3、ApiImplicitParams/ApiImplicitParam方法参数说明
 * */

@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(value = "提供用户登录和注册的接口", tags = "用户管理")
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "username", value = "用户登录账号", required = true),
            @ApiImplicitParam(dataType = "string", name = "password", value = "用户登录密码", required = true),
    })
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ResultVo login(@RequestParam("username") String username,
                          @RequestParam("password") String password) {
        System.out.println(username);
        return userService.checkLoginin(username, password);
    }

    @ApiOperation("用户注册接口")
    @ApiImplicitParam(name = "用户信息", required = false)
    @RequestMapping(value = "/regist", method = RequestMethod.POST)
    public ResultVo regist(Users users) {
        System.out.println(users.getUsername());
        return userService.registerUser(users);
    }
}
